define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        cookie = require('js/utils/cookie'),
        app = Backbone.history;
    require('datetimepicker');
    var FeedBackListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
            // 'click #reply-feedback': 'replyFeedbackBtnClicked',
            'click #closeBtn': 'closeReplyFeedbackDialog',
            'click #saveBtn': 'replyFeedback'
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.FeedBackCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function (e) {
            e.preventDefault();
            if (this.collection.total == 0) {
                return;
            }
            if (this.collection.total > 1000) {
                notify.warning('警告', '数据量较大，请耐心等待');
            }
            utils.exportData(this.collection.url, this.options);
            return false;
        },
        closeReplyFeedbackDialog: function () {
            $("#ReplyFeedbackDialog").hide();
        },
        replyFeedback: function (e) {
            var item_id = $('#item_id').val();
            var feedback_content = this.collection.models[item_id].attributes.content;
            var user_id = this.collection.models[item_id].attributes.user_id;
            var feedback_id = this.collection.models[item_id].attributes.id;
            var that = this;
            $.ajax({
                url: '/admin/feedback/reply/',
                type: 'POST',
                headers: {
                    'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                    'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token')
                },
                data: {
                    'user_id': user_id,
                    'reply_content': $("#reply-content").val(),
                    'feedback_content': feedback_content,
                    'feedback_id': feedback_id
                }
            }).done(function (resp) {
                notify.success('提示', '回复成功！');
                that.closeReplyFeedbackDialog();
                Backbone.history.loadUrl();
            }).fail(function (resp) {
                notify.error('错误', '回复失败！');
                that.closeReplyFeedbackDialog();
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || '-created_at',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/feedback/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD'
            });
            utils.getPaginator(that.options, this.collection.total, '#stats/feedback/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/FeedBackList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new FeedBackListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});

var feedback = {
    replyFeedbackBtnClicked: function(i){
        $('#item_id').val(i);
        $("#ReplyFeedbackDialog").show();
    },
}
